#include "UpdateHandler.h"
#include <iostream>
#include "FileUtil.h"
#include "ConfigUtil.h"
#include <pthread.h>
using namespace std;
UpdateHandler::UpdateHandler(/* args */)
{
}

UpdateHandler::~UpdateHandler()
{
}

void UpdateHandler::handler(TcpSocket *socket,map<string,string> paramMap)
{
    map<string,string> configMap=ConfigUtil::paraConfig("./config/updata.ini","config");
    string version=paramMap["version"];
    string serverVersion=configMap["version"];

    Message *message=new Message;
    message->type=1;

    //相等，传输更新文件的字节流给客户端
    if (version.compare(serverVersion)==0)
    {
        //通过FileContent读取
        FileContent *fileContent=(FileContent*)calloc(1,sizeof(FileContent));
        string path=configMap["path"];
        readFile(fileContent,(char*)path.c_str());

        message->content=(char*)fileContent->content;
        message->length=fileContent->length;
    }else
    {
        cout<<"版本不匹配"<<endl;
        message->length=-1;
    }
    socket->sendMag(message,3000);
    delete message;
    
}

UpdateHandler *UpdateHandler::getInstance()
{
    if (instance==nullptr)
    {
        instance=new UpdateHandler;
    }
    
    return instance;
}

UpdateHandler* UpdateHandler::instance=nullptr;